package com.example.sharesinfo.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.CommonAdapter;
import com.example.sharesinfo.adapter.CommonViewHolder;
import com.example.sharesinfo.bean.TingFuPaiBean;
import com.example.sharesinfo.model.TingFuPaiModel;
import com.example.sharesinfo.utils.CommonUtils;
import com.example.sharesinfo.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class TingFuPaiFragment extends Fragment {

    private int type;
    private TingFuPaiBean mBean;

    //数据填入
    private RecyclerView mTFPView;
    private CommonAdapter<TingFuPaiModel> mTFPAdapter;
    private List<TingFuPaiModel> mList = new ArrayList<>();
    private View item_empty_view;
    public TingFuPaiFragment(int type, TingFuPaiBean bean) {
        this.type = type;
        this.mBean = bean;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ting_fu_pai, null);
        LogUtils.i(mBean.getData().getDate());
        initView(view, type);
        return view;
    }

    private void initView(View view, int type) {
        item_empty_view=view.findViewById(R.id.item_empty_view);
        mTFPView = view.findViewById(R.id.mTingFuPaiView);
        mTFPView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mTFPView.addItemDecoration(new DividerItemDecoration(getActivity(),
                DividerItemDecoration.VERTICAL));
        if (mList.size() > 0) {
            mList.clear();
        }
        switch (type) {
            //   ,
            case 0:
                //"停牌"
                if (CommonUtils.isEmpty(mBean.getData().getStopList())) {
                    //不为空
                    mList.addAll(mBean.getData().getStopList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 1:
                //"复牌"
                if (CommonUtils.isEmpty(mBean.getData().getRecoverList())) {
                    mList.addAll(mBean.getData().getRecoverList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                //"首发上市"
                if (CommonUtils.isEmpty(mBean.getData().getStartList())) {
                    mList.addAll(mBean.getData().getStartList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 3:
                //"网上发行"
                if (CommonUtils.isEmpty(mBean.getData().getNewStockNetPublishList())) {
                    mList.addAll(mBean.getData().getNewStockNetPublishList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 4:
                //"资格登记"
                if (CommonUtils.isEmpty(mBean.getData().getStockholderList())) {
                    mList.addAll(mBean.getData().getStockholderList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 5:
                //"增发新股"
                if (CommonUtils.isEmpty(mBean.getData().getAddNewStockNetPublishList())) {
                    mList.addAll(mBean.getData().getAddNewStockNetPublishList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 6:
                //"分红转增"
                if (CommonUtils.isEmpty(mBean.getData().getShareRegistList())) {
                    mList.addAll(mBean.getData().getShareRegistList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 7:
                // "除权除息"
                if (CommonUtils.isEmpty(mBean.getData().getShareDividendList())) {
                    mList.addAll(mBean.getData().getShareDividendList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 8:
                // "退市风险"
                if (CommonUtils.isEmpty(mBean.getData().getStockAlarmList())) {
                    mList.addAll(mBean.getData().getStockAlarmList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 9:
                //"配股股权"
                if (CommonUtils.isEmpty(mBean.getData().getRationedSharesRegList())) {
                    mList.addAll(mBean.getData().getRationedSharesRegList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case 10:
                // "配股上市"
                if (CommonUtils.isEmpty(mBean.getData().getRationedSharesList())) {
                    mList.addAll(mBean.getData().getRationedSharesList());
                } else {
                    item_empty_view.setVisibility(View.VISIBLE);
                    Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        mTFPAdapter = new CommonAdapter<>(mList, new CommonAdapter.OnBindDataListener<TingFuPaiModel>() {
            @Override
            public void onBindViewHolder(TingFuPaiModel model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setText(R.id.tv_stock_name, model.getName());
                viewHolder.setText(R.id.tv_stock_code, model.getCode());
                viewHolder.setText(R.id.tv_reason, model.getReason());
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.ting_fu_pai_item;
            }
        });
        mTFPView.setAdapter(mTFPAdapter);
    }

}
